<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      default: () => ({
        indicator: [
          { name: '会议效率', max: 100 },
          { name: '参与度', max: 100 },
          { name: '准时率', max: 100 },
          { name: '满意度', max: 100 },
          { name: '完成度', max: 100 }
        ],
        series: [
          {
            name: '本周会议',
            data: [85, 90, 95, 88, 92]
          }
        ]
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler() {
        this.updateChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.updateChart()
    },
    updateChart() {
      if (!this.chart) return
      
      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}',
          textStyle: {
            fontSize: 14,
            color: '#fff'
          },
          backgroundColor: 'rgba(0, 0, 0, 0.8)',
          borderColor: 'rgba(255, 255, 255, 0.2)',
          borderWidth: 1
        },
        legend: {
          data: this.chartData.series.map(item => item.name),
          textStyle: {
            fontSize: 12,
            color: '#fff',
            fontWeight: 'bold'
          },
          itemWidth: 12,
          itemHeight: 12,
          itemGap: 15
        },
        radar: {
          indicator: this.chartData.indicator,
          radius: 60, // 稍微减小半径，为文字留出更多空间
          center: ['50%', '50%'],
          name: {
            textStyle: {
              fontSize: 12,
              color: '#fff',
              fontWeight: 'bold'
            }
          },
          splitArea: {
            show: true,
            areaStyle: {
              color: ['rgba(255, 255, 255, 0.05)', 'rgba(255, 255, 255, 0.1)']
            }
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.3)'
            }
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255, 255, 255, 0.2)'
            }
          }
        },
        series: this.chartData.series.map(item => ({
          name: item.name,
          type: 'radar',
          data: [{
            value: item.data,
            name: item.name,
            itemStyle: {
              color: '#67C23A'
            },
            areaStyle: {
              color: 'rgba(103, 194, 58, 0.3)'
            },
            lineStyle: {
              color: '#67C23A',
              width: 2
            }
          }]
        }))
      }
      
      this.chart.setOption(option)
    }
  }
}
</script>
